/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battleship_server;

import java.util.*;
import battleship_server.threads.*;

/**
 *
 * @author jalejandrovc
 */
public class ServerGameController {

    public FilterManager filterManager;
    public Database database;
    private static ServerGameController gc = null;

    protected ServerGameController() {
        //if (authenticate()) {
            initComponents();
            GameCreationThread gameCreationThread = new GameCreationThread();
            gameCreationThread.start();
        //}
    }

    private boolean authenticate() {
        String username = "", password = "";
        do {
            System.out.println("Username: ");
            Scanner in = new Scanner(System.in);
            username = in.next();
            System.out.println("Password: ");
            password = in.next();
        } while (!username.equals("dionysus") && !password.equals("delriopachecososavilla"));
        System.out.println("Welcome!");
        return true;
    }

    private void initComponents() {
        database = new Database();
        System.out.println("Database connection successfull.");
        filterManager = new FilterManager();
        filterManager.start();
    }

    public static ServerGameController getInstance() {
        if (gc == null) {
            gc = new ServerGameController();
        }
        return gc;
    }

    public void queuePlayer(SocketConnection sc){
        Manager.queuedPlayers.add(sc);
    }

    public boolean lookForPlayer(String player) {
        Iterator i = Manager.onlineUsers.listIterator();
        while (i.hasNext()) {
            SocketConnection sc = (SocketConnection)i.next();
            if (sc.getPlayer().getUser().equals(player)) {
                return true;
            }
        }
        return false;
    }
}
